package com.jg.dao.impl;

import com.jg.dao.AttendanceRecordDao;
import com.jg.dao.BaseDao;
import com.jg.entity.AttendanceRecord;
import com.jg.util.DateUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/3/27 10:29
 */
public class AttendanceRecordDaoImpl extends BaseDao implements AttendanceRecordDao {
    @Override
    public boolean isClockIn(String employeeId) {
        String sql = "SELECT count(1) count FROM `attendance_records` WHERE employee_id = " + employeeId + " AND date = DATE_FORMAT(CURDATE(),'%Y-%m-%d')";
        ResultSet rs = executeQuery(sql);
        if (rs != null) {
            try {
                while (rs.next()) {
                    return rs.getInt("count") == 1;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return false;
    }

    @Override
    public void clockIn(String employeeId) {
        StringBuffer sb = new StringBuffer();
        sb.append("INSERT INTO `attendance_records` (`employee_id`, `check_in_time`, `status`, date) VALUES ");
        sb.append(" ("+employeeId);
        sb.append(" ,Time(NOW()), ");
        sb.append(" CASE ");
        sb.append(" WHEN TIME(NOW()) > '09:00:00' THEN '迟到'" );
        sb.append(" ELSE '正常'" );
        sb.append(" END, ");
        sb.append(" DATE_FORMAT(CURDATE(),'%Y-%m-%d')) ");
        executeUpdate(sb.toString());

    }

    @Override
    public void updateClockIn(String employeeId) {
        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE `attendance_records` SET `check_out_time` = Time(NOW()) ");
        sb.append(" WHERE `employee_id` = "+employeeId);
        sb.append(" AND `date` = DATE_FORMAT(CURDATE(),'%Y-%m-%d') ");
        executeUpdate(sb.toString());

    }

    @Override
    public List<AttendanceRecord> getAttendanceList(String employeeId, String startDate, String endDate) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ar.*,CONCAT(");
        sb.append(" CASE DAYOFWEEK(ar.date)");
        sb.append(" WHEN 1 THEN '星期日'");
        sb.append(" WHEN 2 THEN '星期一'");
        sb.append(" WHEN 3 THEN '星期二'");
        sb.append(" WHEN 4 THEN '星期三'");
        sb.append(" WHEN 5 THEN '星期四'");
        sb.append(" WHEN 6 THEN '星期五'");
        sb.append(" WHEN 7 THEN '星期六'");
        sb.append(" END) AS '日期',");
        sb.append(" CONCAT(FLOOR(TIMESTAMPDIFF(MINUTE, check_in_time, check_out_time)/60),'小时',");
        sb.append(" MOD(TIMESTAMPDIFF(MINUTE, check_in_time, check_out_time), 60),'分钟') AS '工时'");
        sb.append(" FROM `attendance_records` ar WHERE employee_id = ? AND date BETWEEN ? AND ?");
        List<Object> params = new ArrayList<>();
        List<AttendanceRecord> list = new ArrayList<>();
        params.add(employeeId);
        params.add(startDate);
        params.add(endDate);
        ResultSet rs = executeQuery(sb.toString(), params.toArray());
        if (rs != null) {
            try {
                while (rs.next()) {
                    AttendanceRecord attendanceRecord = new AttendanceRecord();
                    attendanceRecord.setId(rs.getInt("id"));
                    attendanceRecord.setEmployeeId(rs.getInt("employee_id"));
                    attendanceRecord.setCheckInTime(rs.getTimestamp("check_in_time"));
                    attendanceRecord.setCheckOutTime(rs.getTimestamp("check_out_time"));
                    attendanceRecord.setStatus(rs.getString("status"));
                    attendanceRecord.setDate(rs.getDate("date"));
                    attendanceRecord.setDate(DateUtil.parseDate(rs.getString("date")));
//                    attendanceRecord.setDate(DateUtil.parseDate(rs.getString("工时")));
                    list.add(attendanceRecord);
                }
            }
            catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                closedAll(rs);
            }
        }
        return list;
    }
}
